//
//  HWAppointmentRankCell.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/3/10.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit

class HWAppointmentRankCell: UITableViewCell {
    var numberButton:UIButton!
    var logoImageView:UIImageView!
    var nameLabel:UILabel!
    var starImageView:UIImageView!
    var starLabel:UILabel!
    var scoreLabel:UILabel!
    var typeLabel:UILabel!
    var sizeLabel:UILabel!
    var detailLabel:UILabel!
    var appointmentButton:UIButton!
    var textTagCollectionView:TTGTextTagCollectionView!
    var timeLabel:UILabel!
    var appointmentButtonClick:((_:String)->())?
    var model:HWAppointmentRankModel? {
        didSet {
            logoImageView.sd_setImage(with: URL(string: model?.icon ?? ""), completed: nil)
            nameLabel.text = model?.name
            scoreLabel.text = model?.point
            typeLabel.text = model?.type
            
            if model?.is_yuyue == true {
                appointmentButton.setTitleColor(.colorWithHexString(hexString: "#CCCCCC"), for: .normal)
                appointmentButton.layer.borderColor = UIColor.colorWithHexString(hexString: "#CCCCCC").cgColor
                appointmentButton.setTitle("已预约", for: .normal)
                appointmentButton.snp.updateConstraints { make in
                    make.width.equalTo(50*widthScale)
                }
            }else {
                appointmentButton.setTitle("预约", for: .normal)
                appointmentButton.setTitleColor(.colorWithHexString(hexString: "#1BC19C"), for: .normal)
                appointmentButton.layer.borderColor = UIColor.colorWithHexString(hexString: "#1BC19C").cgColor
                appointmentButton.snp.updateConstraints { make in
                    make.width.equalTo(40*widthScale)
                }
            }
            
            textTagCollectionView.removeAllTags()
            for (idx,txt) in (model?.biaoqian ?? []).enumerated() {
                let content = TTGTextTagStringContent.init(text: txt)
                content.textFont = UIFont.systemFont(ofSize: 8*widthScale)
                let normalStyle = TTGTextTagStyle.init()
                normalStyle.cornerRadius = 2*widthScale
                normalStyle.extraSpace = CGSize.init(width: 8*widthScale, height: 2*widthScale)
                normalStyle.backgroundColor = .white
                normalStyle.shadowColor = .white
                
                if idx == 0 {
                    content.textColor = UIColor.colorWithHexString(hexString: "#FD9450")
                    normalStyle.borderColor = UIColor.colorWithHexString(hexString: "#FE6A4C")
                }else if idx == 1 {
                    content.textColor = UIColor.colorWithHexString(hexString: "#2B66FB")
                    normalStyle.borderColor = UIColor.colorWithHexString(hexString: "#2B66FB")
                }else if idx == 2 {
                    content.textColor = UIColor.colorWithHexString(hexString: "#7936FB")
                    normalStyle.borderColor = UIColor.colorWithHexString(hexString: "#7936FB")
                }else if idx == 3 {
                    content.textColor = UIColor.colorWithHexString(hexString: "#444444")
                    normalStyle.borderColor = UIColor.colorWithHexString(hexString: "#444444")
                }else{
                    content.textColor = UIColor.colorWithHexString(hexString: "#CEA224")
                    normalStyle.borderColor = UIColor.colorWithHexString(hexString: "#CEA224")
                }
                
                let tag = TTGTextTag.init()
                tag.content = content
                tag.style = normalStyle
                textTagCollectionView.addTag(tag)
                
            }
        }
    }
    
    var indexPath:IndexPath?{
        didSet {
            numberButton.setTitle("", for: .normal)
            if indexPath?.row == 0 {
                numberButton.setImage(UIImage(named: "panhangbandiyiming"), for: .normal)
            } else if indexPath?.row == 1 {
                numberButton.setImage(UIImage(named: "dierming2"), for: .normal)
            } else if indexPath?.row == 2 {
                numberButton.setImage(UIImage(named: "panhangdisan3"), for: .normal)
            } else{
                numberButton.setImage(nil, for: .normal)
                numberButton.setTitle("\(indexPath!.row + 1)" , for: .normal)
            }
        }
    }
    class func cellWithTableView(tableView:UITableView) -> (HWAppointmentRankCell) {
        let reUse:String = "HWAppointmentRankCell"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
        
        if cell == nil {
            cell = HWAppointmentRankCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
        }
        
        return cell as! HWAppointmentRankCell
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
        initFrame()
    }
    
    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = UIColor.clear
        
        numberButton = UIButton(type: .custom)
        numberButton.titleLabel?.font = .boldSystemFont(ofSize: 14*widthScale)
        numberButton.setTitleColor(.colorWithHexString(hexString: "#444444"), for: .normal)
        contentView.addSubview(numberButton)
        logoImageView = UIImageView()
        logoImageView.backgroundColor = .clear
        logoImageView.layer.cornerRadius = 10*widthScale
        logoImageView.layer.masksToBounds = true
        logoImageView.contentMode = .scaleAspectFill
        logoImageView.backgroundColor = .red
        contentView.addSubview(logoImageView)
        
        nameLabel = HWLabel.createBoldLabel(title: "-", color: "#444444", font: 14*widthScale)
        nameLabel.numberOfLines = 1
        contentView.addSubview(nameLabel)
        
        starImageView = UIImageView()
        starImageView.image = UIImage(named: "homeStar")
        contentView.addSubview(starImageView)
        
        scoreLabel = HWLabel.createBoldLabel(title: "-", color: "#999999", font: 10*widthScale)
        contentView.addSubview(scoreLabel)
        
        typeLabel = HWLabel.createBoldLabel(title: "-", color: "#999999", font: 10*widthScale)
        contentView.addSubview(typeLabel)
        
        textTagCollectionView = TTGTextTagCollectionView.init(frame: .zero)
        textTagCollectionView.delegate = self;
        textTagCollectionView.alignment = TTGTagCollectionAlignment.left;
        textTagCollectionView.manualCalculateHeight = true;
        textTagCollectionView.numberOfLines = 1
        textTagCollectionView.scrollDirection = .horizontal
        textTagCollectionView.showsHorizontalScrollIndicator = false
        contentView.addSubview(textTagCollectionView)
        
//        detailLabel = HWLabel.createLabel(title: "详情", color: "#1BC19C", font: 12*widthScale)
//        detailLabel.layer.borderColor = UIColor.colorWithHexString(hexString: "#1BC19C").cgColor
//        detailLabel.layer.borderWidth = 1*widthScale
//        detailLabel.layer.cornerRadius = 13*widthScale
//        detailLabel.textAlignment = .center
//        contentView.addSubview(detailLabel)
        
        appointmentButton = UIButton(type: .custom)
        appointmentButton.setTitle("预约", for: .normal)
        appointmentButton.setTitleColor(.colorWithHexString(hexString: "#1BC19C"), for: .normal)
        appointmentButton.titleLabel?.font = .systemFont(ofSize: 12*widthScale)
        appointmentButton.layer.borderColor = UIColor.colorWithHexString(hexString: "#1BC19C").cgColor
        appointmentButton.layer.borderWidth = 1*widthScale
        appointmentButton.layer.cornerRadius = 13*widthScale
        appointmentButton.addTarget(self, action: #selector(appointmentButtonClick(_:)), for: .touchUpInside)
        contentView.addSubview(appointmentButton)
        
        
        timeLabel = HWLabel.createBoldLabel(title: "-", color: "#CCCCCC", font: 10*widthScale)
        contentView.addSubview(timeLabel)
        
        timeLabel.setContentCompressionResistancePriority(.required, for: .horizontal)
        timeLabel.setContentHuggingPriority(.required, for: .horizontal)
    }
    
    func initFrame() -> Void {
        
        numberButton.snp.makeConstraints { make in
            make.left.equalTo(20*widthScale)
            make.centerY.equalToSuperview()
            make.width.width.equalTo(30*widthScale)
            make.height.equalTo(numberButton.snp.width).multipliedBy(1.1)
        }
        
        logoImageView.snp.makeConstraints { make in
            make.left.equalTo(numberButton.snp.right).offset(10*widthScale)
            make.top.equalTo(10*widthScale)
            make.width.height.equalTo(52*widthScale)
            make.bottom.equalTo(-10*widthScale)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(logoImageView.snp.right).offset(6*widthScale)
            make.top.equalTo(logoImageView)
            make.right.equalTo(timeLabel.snp.left).offset(-5)
        }
        
        starImageView.snp.makeConstraints { make in
            make.width.height.equalTo(8*widthScale)
            make.left.equalTo(nameLabel)
            make.top.equalTo(nameLabel.snp.bottom).offset(6*widthScale)
        }
        
        scoreLabel.snp.makeConstraints { make in
            make.left.equalTo(starImageView.snp.right)
            make.centerY.equalTo(starImageView)
        }
        
        typeLabel.snp.makeConstraints { make in
            make.left.equalTo(scoreLabel.snp.right).offset(6*widthScale)
            make.centerY.equalTo(scoreLabel)
        }
        
        textTagCollectionView.snp.makeConstraints { make in
            make.left.equalTo(nameLabel)
            make.right.equalTo(0)
            make.bottom.equalTo(logoImageView)
        }
        
        appointmentButton.snp.makeConstraints { make in
            make.bottom.equalTo(logoImageView)
            make.right.equalTo(-20*widthScale)
            make.width.equalTo(40*widthScale)
            make.height.equalTo(26*widthScale)
        }
        
        timeLabel.snp.makeConstraints { make in
            make.right.equalTo(appointmentButton)
            make.centerY.equalTo(nameLabel)
        }
    }
    
    @objc func appointmentButtonClick(_:UIButton) -> Void {
        appointmentButtonClick?(model?.id ?? "")
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
extension HWAppointmentRankCell:TTGTextTagCollectionViewDelegate{
    func textTagCollectionView(_ textTagCollectionView: TTGTextTagCollectionView!, didTap tag: TTGTextTag!, at index: UInt) {
        //        tagIsSelect = tag.selected
        //        for i in 0..<_model!.datas.count {
        //
        //            textTagCollectionView.updateTag(at: UInt(i), selected: false)
        //            _model?.datas[i].isSelect = false
        //        }
        //        textTagCollectionView.updateTag(at: index, selected: tagIsSelect)
        //        _model?.datas[Int(index)].isSelect = tagIsSelect
        
    }
}

